import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/Login'
import home from '@/views/Home'
import welcome from '@/views/welcome/welcome'
import User from '@/views/user/user'
import Rigths from '@/views/rigth/Rigths'
import Roles from '@/views/rigth/Roles'
Vue.use(Router)


 var router= new Router({
  routes: [
    {name:"Login",path:"/login",component:login},
    {name:"Home",path:"/",component:home,
    redirect:{path:'/welcome'},
    children:[
      {name:"Welcome",path:"welcome",component:welcome},
      {name:'user',path:"user",component:User},
      {name:'Rigths',path:'rigths',component:Rigths},
      {name:Roles,path:'roles',component:Roles}
    ]}
  ]
})


//注册全局守卫 作用是在路由跳转前对路由进行判断 防止未登录的用户跳转到需要登录的页面
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('mytoken')
  //如果已经登录 放行  
  if(token){
    next()
    //如果没有登录 访问非登录页面 则跳转登录页面
  }else if(to.path!=='/login'){
    next({path:"/login"})
    // 如果没有登录 访问登录页面 直接进入
  }else{
    next()
  }
})

export default router